<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.name" v-model="listQuery.name" style="width: 200px;" class="filter-item" placeholder="日志名称" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-if="formThead.name" align="center" label="日志名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.content" align="center" label="日志内容">
        <template slot-scope="scope">
          <span><logcontent :row_data="scope.row" @getLists="getLists"/></span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.system" align="center" label="系统">
        <template slot-scope="scope">
          <span>{{ scope.row.system }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.module" align="center" label="模块">
        <template slot-scope="scope">
          <span>{{ scope.row.module }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.func" align="center" label="方法">
        <template slot-scope="scope">
          <span>{{ scope.row.func }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.addtime" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.addtime | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button icon="el-icon-delete" type="danger" circle @click="del(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getList, del } from '@/api/log/log'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import { parseTime } from '@/utils/index'
import logcontent from './logcontent'
export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { logcontent },
  filters: {
    formatDate: function(time) {
      if (!time) {
        return ''
      }
      return parseTime(time)
    }
  },
  data() {
    return {
      users: null,
      listItemId: null,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        name: undefined,
        content: undefined,
        system: undefined,
        module: undefined,
        func: undefined,
        addtime: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        name: true,
        content: true,
        system: true,
        module: true,
        func: true,
        addtime: true
      }
    }
  },
  created() {
    this.getLists()
  },
  methods: {
    getLists() {
      this.listLoading = true
      getList(store.getters.token, this.listQuery).then(response => {
        this.list = response.data.list.data
        this.total = response.data.list.total
        this.listLoading = false
        this.listQuery.start_time = null
      })
    },
    handleFilter() {
      if (this.listQuery.start_time !== null) {
        this.listQuery.start_time[0] = this.listQuery.start_time[0] / 1000
        this.listQuery.start_time[1] = this.listQuery.start_time[1] / 1000
      }
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('此操作将会删除该记录,是否继续', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        del(store.getters.token, { id: id }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getLists()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '删除失败!'
        })
      })
    }
  }
}
</script>
